home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / lib / flyEngine / flyConsole.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-12  |  10.5 KB  |  549 lines

  1. #include "../Fly3D.h"
  2.  
  3. console::console()
  4. {
  5.     memset(buf,0,sizeof(char *)*CONSOLE_MAX_LINES);
  6.     nlines=0;
  7.     cmd_line[0]=0;
  8.     cmd_pos=0;
  9.     mode=0;
  10.     winlines=0;
  11.     time=0;
  12.     linecount=0;
  13. }
  14.  
  15. console::~console()
  16. {
  17.     int i;
  18.     for( i=0;i<nlines;i++ )
  19.         if (buf[i])
  20.             delete buf[i];
  21. }
  22.  
  23. void console::step(int dt)
  24. {
  25.     time-=dt;
  26.     if (time<0)
  27.     {
  28.         time=20;
  29.         if (mode>0)
  30.         {
  31.             if (winlines<CONSOLE_LIN/3)
  32.                 winlines++;
  33.         }
  34.         else if (mode<0)
  35.         {
  36.             if (winlines>0)
  37.                 winlines--;
  38.             if (winlines==0)
  39.             {
  40.                 mode=0;
  41.                 flyengine->noinput=0;
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. void console::show()
  48. {
  49.     mode=1;
  50.     flyengine->noinput=1;
  51.     cmd_line[cmd_pos=0]=0;
  52. }
  53.  
  54. void console::hide()
  55. {
  56.     mode=-1;
  57.     cmd_line[cmd_pos=0]=0;
  58. }
  59.  
  60. void console::add_string(char *fmt, ...)
  61. {
  62.     static char str[256];
  63.  
  64.     va_list va;
  65.     va_start( va, fmt );
  66.     wvsprintf( str, fmt, va );
  67.     va_end( va );
  68.     strupr( str );
  69.  
  70.     if (nlines==CONSOLE_MAX_LINES)
  71.         delete buf[--nlines];
  72.     memmove(&buf[1],&buf[0],sizeof(char *)*nlines);
  73.     int len=strlen(str);
  74.     buf[0]=new char[len+1];
  75.     memcpy(buf[0],str,len+1);
  76.     nlines++;
  77.     linecount++;
  78. }
  79.  
  80. void console::draw()
  81. {
  82.     int i;
  83.     float f1,f2;
  84.  
  85.     dx=(float)screen_sx/CONSOLE_COL;
  86.     dy=(float)screen_sy/CONSOLE_LIN;
  87.  
  88.     f1=CONSOLE_LIN/3*dy;
  89.     f2=winlines*dy;
  90.  
  91.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  92.     tc->use(flyengine->consolepic);
  93.     glBegin(GL_QUADS);
  94.         glTexCoord2f(1,0);
  95.         glVertex2f((float)screen_sx,screen_sy+(f1-f2));
  96.         glTexCoord2f(0,0);
  97.         glVertex2f(0,screen_sy+(f1-f2));
  98.         glTexCoord2f(0,1);
  99.         glVertex2f(0,screen_sy-f2-4);
  100.         glTexCoord2f(1,1);
  101.         glVertex2f((float)screen_sx,screen_sy-f2-4);
  102.     glEnd();
  103.  
  104.     glBlendFunc(GL_ONE,GL_ONE);
  105.     tc->use(flyengine->fontspic);
  106.     for( i=0;i<winlines-1;i++ )
  107.         if (winlines-i-2<nlines)
  108.             draw_text(dx,i*dy,buf[winlines-i-2]);
  109.  
  110.     f1=(float)draw_text(dx,i*dy,cmd_line)+1;
  111.  
  112.     tc->use(-1);
  113.     unsigned char uc=abs((flyengine->cur_time%500)-250);
  114.     glColor3ub(uc,uc,uc);
  115.     glBegin(GL_QUADS);
  116.         glVertex2f(f1,screen_sy-i*dy-dy);
  117.         glVertex2f(f1+dx/8,screen_sy-i*dy-dy);
  118.         glVertex2f(f1+dx/8,screen_sy-i*dy);
  119.         glVertex2f(f1,screen_sy-i*dy);
  120.     glEnd();
  121. }
  122.  
  123. float console::draw_text(float x,float y,char *text)
  124. {
  125.     int len=strlen(text),i;
  126.     float xx,yy,xy=1.0f/FONTS_NUM;
  127.  
  128.     y=screen_sy-y-dy;
  129.  
  130.     for( i=0;i<len;i++ )
  131.         if (text[i]>=32 && text[i]<96)
  132.         {
  133.         xx=((text[i]-32)%FONTS_NUM)*xy;
  134.         yy=((text[i]-32)/FONTS_NUM)*xy;
  135.  
  136.         glBegin(GL_QUADS);
  137.         glTexCoord2f(xx+xy,yy+xy);
  138.         glVertex2f(x+dx,y);
  139.  
  140.         glTexCoord2f(xx+xy,yy);
  141.         glVertex2f(x+dx,y+dy);
  142.  
  143.         glTexCoord2f(xx,yy);
  144.         glVertex2f(x,y+dy);
  145.  
  146.         glTexCoord2f(xx,yy+xy);
  147.         glVertex2f(x,y);
  148.         glEnd();
  149.  
  150.         x+=flyengine->fonts_width[text[i]-32]*dx/FONTS_SIZE;
  151.         }
  152.     
  153.     return x;
  154. }
  155.  
  156. void console::key_press(int key)
  157. {
  158.     if (key==VK_ESCAPE)
  159.         if (mode==0)
  160.             show();
  161.         else hide();
  162.     else 
  163.     if (key==VK_RETURN)
  164.         {
  165.             command_exec(cmd_line);
  166.             cmd_line[0]=0;
  167.             cmd_pos=0;
  168.         }
  169.     else
  170.     if (key==8)
  171.         {
  172.             if (cmd_pos>0)
  173.                 cmd_line[--cmd_pos]=0;
  174.         }
  175.     else
  176.     if (mode)
  177.     {
  178.         if (key>='a' && key<='z')
  179.             key-='a'-'A';
  180.         if (key>=32 && key<96 && cmd_pos<80) 
  181.         {
  182.             cmd_line[cmd_pos++]=key;
  183.             cmd_line[cmd_pos]=0;
  184.         }
  185.     }
  186. }
  187.  
  188. int console::command_tokens(char *str,char **token)
  189. {
  190.     int ntoken=0;
  191.  
  192.     do
  193.     {
  194.     while( *str==' ' )
  195.         *(str++)=0;
  196.     if (*str==0)
  197.         break;
  198.     token[ntoken]=str;
  199.  
  200.     str=strchr(str,' ');
  201.     if (token[ntoken][0]=='(')
  202.         {
  203.         token[ntoken]++;
  204.         str=strchr(token[ntoken],')');
  205.         if (str)
  206.             *(str++)=0;
  207.         }
  208.     if (token[ntoken][0]=='\"')
  209.         {
  210.         token[ntoken]++;
  211.         str=strchr(token[ntoken],'\"');
  212.         if (str)
  213.             *(str++)=0;
  214.         }
  215.  
  216.     ntoken++;
  217.     }while(str);
  218.  
  219.     return ntoken;
  220. }
  221.  
  222. void console::command_exec(char *str) 
  223. {
  224.     char *token[256],buf[256];
  225.     int ntoken;
  226.     int i,j,k;
  227.  
  228.     strcpy(buf,">");
  229.     strcat(buf,str);
  230.     add_string(buf);
  231.     strlwr(buf);
  232.  
  233.     ntoken=command_tokens(&buf[1],token);
  234.  
  235.     if (ntoken==0)
  236.         {
  237.         add_string("Invalid command!");
  238.         return;
  239.         }
  240.  
  241.     if (!strcmp(token[0],"help") || token[0][0]=='?')
  242.         {
  243.         add_string("commands:");
  244.         add_string(" get [ varname | objname.objparam ]");
  245.         add_string(" set [ varname | objname.objparam ] val");
  246.         add_string(" map [ path/level.fly ]");
  247.         add_string(" connect");
  248.         add_string(" save");
  249.         add_string(" reinit");
  250.         add_string(" list [ \"plugins\" | \"classes\" | classname | objname ]");
  251.         add_string(" create classname objname");
  252.         add_string(" destroy objname");
  253.         add_string(" activate objname");
  254.         add_string(" insert plugin.dll");
  255.         add_string(" remove plugin.dll");
  256.         add_string(" exec commandline");
  257.         add_string(" quit");
  258.         }
  259.     else
  260.     if (!strcmp(token[0],"map"))
  261.     {
  262.         if (ntoken==1)
  263.             add_string("map: %s",flyengine->flyfile[0]==0?"(none)":flyengine->flyfile);
  264.         else
  265.             {
  266.             flyengine->close_multiplayer();
  267.             flyengine->open_fly_file(token[1]);
  268.             add_string("map: %s",flyengine->flyfile[0]==0?"(none)":flyengine->flyfile);
  269.             }
  270.     }
  271.     else
  272.     if (!strcmp(token[0],"quit"))
  273.         {
  274.         DestroyWindow(hFlyWnd);
  275.         }
  276.     else
  277.     if (!strcmp(token[0],"exec"))
  278.         {
  279.         char c[256];
  280.         strcpy(c,flyengine->flysdkpath);
  281.         strcat(c,token[1]);
  282.         WinExec(c,SW_SHOW);
  283.         }
  284.     else
  285.     if (!strcmp(token[0],"connect"))
  286.         {
  287.         flyengine->join_multiplayer();
  288.         add_string("map: %s",flyengine->flyfile[0]==0?"(none)":flyengine->flyfile);
  289.         }
  290.     else
  291.     if (!strcmp(token[0],"insert"))
  292.         {
  293.         if (ntoken==1)
  294.             add_string("Missing parameters!");
  295.         else
  296.         {
  297.             i=flyengine->dll.add_dll(token[1]);
  298.             if (i==-1)
  299.                 add_string("Invalid plugin dll!");
  300.             else flyengine->dll.load_classes(i,0);
  301.         }
  302.     }
  303.     else
  304.     if (!strcmp(token[0],"remove"))
  305.         {
  306.         if (ntoken==1)
  307.             add_string("Missing parameters!");
  308.         else
  309.             if (flyengine->dll.delete_dll(token[1])==0)
  310.                 add_string("Invalid plugin dll!");
  311.         }
  312.     else
  313.     if (!strcmp(token[0],"create"))
  314.         {
  315.         if (ntoken<3)
  316.             add_string("Missing parameters!");
  317.         else
  318.             {
  319.             bsp_object *obj=flyengine->dll.add_class(token[1]);
  320.             if (obj==0)
  321.                 add_string("Invalid class name!");
  322.             else
  323.                 {
  324.                 strcpy(obj->long_name,token[2]);
  325.                 obj->name[0]=0;
  326.                 }
  327.             }
  328.         }
  329.     else
  330.     if (!strcmp(token[0],"destroy"))
  331.         {
  332.         if (ntoken<2)
  333.             add_string("Missing parameters!");
  334.         else
  335.             {
  336.             bsp_object *o=flyengine->active_obj0;
  337.             while(o)
  338.                 {
  339.                 if (!stricmp(o->long_name,token[1]))
  340.                     break;
  341.                 o=(bsp_object *)o->next_obj;
  342.                 }
  343.             if (o==0)
  344.                 add_string("Invalid class name!");
  345.             else 
  346.                 o->life=-1;
  347.             }
  348.         }
  349.     else
  350.     if (!strcmp(token[0],"list"))
  351.         {
  352.         char s[256]="";
  353.         if (ntoken==1)
  354.         {
  355.             param_desc pd;
  356.             j=flyengine->get_global_param_desc(0,0);
  357.             for( k=0;k<j;k++ )
  358.                 {
  359.                 flyengine->get_global_param_desc(k,&pd);
  360.                 strcpy(s,pd.name);
  361.                 strcat(s,"=");
  362.                 strcat(s,pd.get_string());
  363.                 add_string(s);
  364.                 }
  365.         }
  366.         else
  367.         if (!strcmp(token[1],"plugins"))
  368.             {
  369.             for( i=0;i<flyengine->dll.ndll;i++ )
  370.                 add_string(flyengine->dll.dll[i]->dll_filename);
  371.             }
  372.         else
  373.         if (!strcmp(token[1],"classes"))
  374.             {
  375.             for( i=0;i<flyengine->dll.ncd;i++ )
  376.                 add_string(flyengine->dll.cd[i]->get_name());
  377.             }
  378.         else
  379.         if (strchr(token[1],'.'))
  380.             {
  381.             char *c=strchr(token[1],'.');
  382.             *c=0;
  383.             bsp_object *o=flyengine->stock_obj0;
  384.             while(o)
  385.                 {
  386.                 if (!stricmp(o->long_name,token[1]))
  387.                     break;
  388.                 o=(bsp_object *)o->next_obj;
  389.                 }
  390.             if (o)
  391.                 {
  392.                 param_desc pd;
  393.                 j=o->get_param_desc(0,0);
  394.                 for( k=0;k<j;k++ )
  395.                     {
  396.                     o->get_param_desc(k,&pd);
  397.                     if (!stricmp(c+1,pd.name))
  398.                         {
  399.                         strcpy(s,pd.name);
  400.                         strcat(s,"=");
  401.                         strcat(s,pd.get_string());
  402.                         add_string(s);
  403.                         break;
  404.                         }
  405.                     }
  406.                 if (k==j)
  407.                     add_string("Invalid object parameter!");
  408.                 }
  409.             else add_string("Invalid object name!");
  410.             }
  411.         else
  412.             {
  413.             for( i=0;i<flyengine->dll.ncd;i++ )
  414.                 if (!strcmp(token[1],flyengine->dll.cd[i]->get_name()))
  415.                     break;
  416.             if (i<flyengine->dll.ncd)
  417.                 {
  418.                 j=flyengine->dll.cd[i]->get_type();
  419.                 bsp_object *o=flyengine->stock_obj0;;
  420.                 while(o)
  421.                     {
  422.                     if (o->type==j)
  423.                         add_string(o->long_name);
  424.                     o=(bsp_object *)o->next_obj;
  425.                     }
  426.                 }
  427.             else
  428.                 {
  429.                 bsp_object *o=flyengine->stock_obj0;
  430.                 while(o)
  431.                     {
  432.                     if (!stricmp(o->long_name,token[1]))
  433.                         break;
  434.                     o=(bsp_object *)o->next_obj;
  435.                     }
  436.                 if (o==0)
  437.                     add_string("Invalid class/object name!");
  438.                 else 
  439.                     {
  440.                     param_desc pd;
  441.                     j=o->get_param_desc(0,0);
  442.                     for( k=0;k<j;k++ )
  443.                         {
  444.                         o->get_param_desc(k,&pd);
  445.                         strcpy(s,pd.name);
  446.                         strcat(s,"=");
  447.                         strcat(s,pd.get_string());
  448.                         add_string(s);
  449.                         }
  450.                     }
  451.                 }
  452.             }
  453.         }
  454.     else
  455.     if (!strcmp(token[0],"save"))
  456.         {
  457.         flyengine->save_fly_file(flyengine->flyfile);
  458.         add_string("Saved!");
  459.         return;
  460.         }
  461.     else
  462.     if (!strcmp(token[0],"set"))
  463.     {
  464.         if (ntoken<3)
  465.         {
  466.             add_string("Missing parameters!");
  467.             return;
  468.         }
  469.  
  470.         if (strchr(token[1],'.'))
  471.         {
  472.             char *c=strchr(token[1],'.');
  473.             *c=0;
  474.             i=flyengine->set_obj_param(token[1],c+1,token[2]);
  475.             if (i==1)
  476.                 add_string("Invalid object name!");
  477.             else if (i==2) 
  478.                 add_string("Invalid object parameter!");
  479.             else if (i==3) 
  480.                 add_string("Unsuported parameter type!");
  481.             return;
  482.         }
  483.  
  484.         if (flyengine->set_global_param(token[1],token[2])==0)
  485.             add_string("Invalid parameter name!");
  486.     }
  487.     else if (!strcmp(token[0],"activate"))
  488.     {
  489.         bsp_object *o=flyengine->stock_obj0;
  490.         while(o)
  491.         {
  492.             if (!stricmp(token[1],o->long_name))
  493.                 break;
  494.             o=(bsp_object *)o->next_obj;
  495.         }
  496.         if (o)
  497.             flyengine->activate(o->clone());
  498.         else add_string("Invalid object name!");
  499.     }
  500.     else if (!strcmp(token[0],"get"))
  501.     {
  502.         char s[256]="";
  503.         if (!strncmp(token[1],"obj(",4))
  504.         {
  505.             char *objname=&token[1][4];
  506.             char *objparam=strchr(token[1],'.');
  507.             char *c=strchr(objname,')');
  508.             if (c==0 || objparam==0)
  509.                 {
  510.                 add_string("Invalid sintax!");
  511.                 return;
  512.                 }
  513.             *c=0;
  514.             i=flyengine->get_obj_param(objname,objparam+1,s);
  515.             if (i)
  516.             {
  517.             if (i==1)
  518.                 add_string("Invalid object name!");
  519.             else if (i==2) 
  520.                 add_string("Invalid object parameter!");
  521.             else if (i==3) 
  522.                 add_string("Unsuported parameter type!!");
  523.             return;
  524.             }
  525.         }
  526.         else
  527.         {
  528.             i=flyengine->get_global_param_desc(0,0);
  529.             param_desc pd;
  530.             for( j=0;j<i;j++ )
  531.                 {
  532.                 flyengine->get_global_param_desc(j,&pd);
  533.                 if (!stricmp(token[1],pd.name))
  534.                     break;
  535.                 }
  536.             if (j==i)
  537.                 {
  538.                 add_string("Invalid variable name!!");
  539.                 return;
  540.                 }
  541.             else strcpy(s,pd.get_string());
  542.         }
  543.         add_string(s);
  544.     }
  545.     else 
  546.         add_string("Invalid command!!");
  547. }
  548.  
  549.